<?php
/**
 * @package panel
 * @subpackage panel.environment.loggers
 */
/**
 * Mailer for error reports
 * @author Dmitry Bakaleinik (code)
 */
class panel__logger__mailer
	implements maf__i__singletone {

	protected $mailer;
	protected $recipients;
	protected $error;

	/**
	 * @return panel__logger__mailer
	 */
	static public function getInstance() {

		static $instance;

		if( !isset($instance) ) {
			$instance = new self; }

		return $instance;

	}	# Function

	protected function __clone() {
	}	# Function

	protected function __sleep() {
	}	# Function

	protected function __wakeup() {
	}	# Function

	protected function __construct() {

		if( !panel__settings__mail::isEnabled() ) {
			return; }

		try {

			$this->mailer = new Swift( panel__settings__mail::getConnection() );

		} catch( exception $e ) {

			$this->error = TRUE;

			panel__logger__errors::getInstance()->logException($e);

		}	# try

	}	# Function

	public function log($_subject_, $_message_) {

		if( $this->error || !panel__settings__mail::isEnabled() ) {
			return; }

		try {

			$this->mailer->send(
				new Swift_Message($_subject_, $_message_),
				panel__settings__mail::getRecipients(),
				sprintf(
					"%s@%s",
					panel__settings__mail::getConnection()->getUsername(),
					panel__settings__mail::getConnection()->getServer()
				)
			);

		} catch( exception $e ) {

			$this->error = TRUE;

			panel__logger__errors::getInstance()->logException($e);

		}	# try

	}	# Function

}	# Class

/* EOF */